<?php

declare(strict_types=1);

namespace app\admin\command;


use app\common\model\BasketballCompetitions;
use app\common\model\BasketballFixtures;
use app\common\model\FootballCompetitions;
use app\common\model\FootballFixtures;
use app\common\model\MatchesSeason;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Fixtures extends Command
{
    protected $model;
    //赛程  跑一次就行   每年的赛程 按理来说就是固定排期好的
    protected function configure()
    {
        // 指令配置 
        $this->setName('fixtures')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->setDescription('赛程');
    }

    protected function execute(Input $input, Output $output)
    {
        //参数接收
        $action = trim($input->getArgument('action'));
        if ($action === 'football') {
            $this->model = new FootballCompetitions(); //足球联赛表
        } else {
            $this->model = new BasketballCompetitions();//蓝球联赛表
        }

        $this->model->chunk(1, function ($models) use ($action, $output) {
            foreach ($models as $model) {
                $output->writeln('============进============');
                $seaon = MatchesSeason::field('season_id')->where(['competition_id' => $model['api_id'], 'type' => $action, 'is_current' => 1])->find(); //年份
                if (!empty($seaon)) {
                    $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/competition/fixtures?competition_id=' . $model['api_id'] . '&season_id=' . $seaon["season_id"];
           
                    $res = curl_get($url);
                    $arr = json_decode($res, true);
                    if ($arr['code'] == 0) {
                        if (!empty($arr['data'])) {
                            $output->writeln('============进DATA============');
                            foreach ($arr['data'] as $key => $argv) {
                                foreach ($argv as $k => $v) {
                                    if($action == 'football'){
                                        $output->writeln('============足球============');
                                        $output->writeln('============' . $model->id . '============');
                                        $is_find = FootballFixtures::where(['foreign_key_id' => $model->foreign_key_id])->find();
                                        $v['foreign_key_id'] = $model->id;
                                
                                        $output->writeln('=========================' . $v['home_corn'] . '=====================================');
                                        if (empty($is_find)) {
                                            FootballFixtures::create($v);
                                            $str = 'add';
                                        } else {
                                            //更新
                                            $is_find->save($v);
                                            $str = 'update';
                                        }
                                        $output->writeln('=========================Football' . $str . '完成=====================================');
                                    }else{
                                        $output->writeln('============篮球============');
                                        $output->writeln('============' . $model->id . '============');
                                        $is_find = BasketballFixtures::where(['foreign_key_id' => $model->foreign_key_id])->find();
                                        $v['foreign_key_id'] = $model->id;
                                        $v['api_id'] =$v['id'];
                                        unset($v['id']);
                                        if (empty($is_find)) {
                                            BasketballFixtures::create($v);
                                            $str = 'add';
                                        } else {
                                            //更新
                                            $is_find->save($v);
                                            $str = 'update';
                                        }
                                        $output->writeln('=========================Football' . $str . '完成=====================================');
                                    }
                                   
                                }
                            }
                        }
                    } else {
                        $output->writeln('api参数出错');
                    }
                } else {
                    $output->writeln('年份无法获取');
                }
            }
        });
        // 指令输出
        $output->writeln('fixtures');
    }
}
